package com.dfrz.bookstore.service.impl;

import com.dfrz.bookstore.dao.impl.BookDaoImpl;
import com.dfrz.bookstore.entry.Book;
import com.dfrz.bookstore.entry.User;
import com.dfrz.bookstore.service.BookService;
import com.dfrz.bookstore.util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类功能描述:
 *
 * @Author keyan
 * @Date 2021/5/19 10:15
 */
public class BookServiceImpl implements BookService {
    private BookDaoImpl bookDao = new BookDaoImpl();

    @Override
    public void inertBook(Book book) {
        List<Book> book2 = bookDao.findObjByObj(book);
        if (book2.size()==0) {
            boolean flag = bookDao.insert(book);
            if (flag) {
                /*这里渲染到前台*/
                System.out.println("插入成功");
            } else {
                System.out.println("插入失败");
            }
        } else {
            System.out.println("书以存在");
        }
    }

    @Override
    public void delectBookById(int id) {
        Book book = new Book();
        book.setId(id);
        List<Book> books = bookDao.findObjByObj(book);

        if (books != null) {
            book = books.get(0);
            //书本下架才能删除
            if (book.getFlag()==0){
                boolean flag = bookDao.deleteObjById(id);
                if (flag) {
                    System.out.println("删除成功");
                } else {
                    System.out.println("删除失败");
                }
            }else {
                System.out.println(book.getName()+"正在上架状态,无法删除");
            }

        } else {
            System.out.println("没有找到需要删除的图书");
        }
    }

    @Override
    public void upDataBook(Book book) {
        boolean flag = bookDao.updateByObj(book);
        if (flag){
            System.out.println("更新成功");
        }else {
            System.out.println("更新失败");
        }
    }

    @Override
    public List<Book> findBookByBook(Book book) {
        List<Book> bookList = bookDao.findObjByObj(book);
        return bookList;
    }

    @Override
    public List<Book> findBookByPage(Book book, int pageNum, int pageSize) {
        List<Book> bookList = bookDao.findObjByPage(book, pageNum, pageSize);
        return bookList;
    }

    /**
     * 根据条件 计算书本总数
     * @return
     */
    public int findCount(Book book, Object object){
//        if (user!=null){
//            book.setFlag(1);
//        }else {
//            book.setFlag(0);
//        }
        int count = bookDao.findCount(book,object);
        return count;
    }
}
